package com.ruoyi.neo.repository;

import com.ruoyi.neo.domain.PartView;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PartViewRepository extends Neo4jRepository<PartView,String> {


    /**
     * 根据零件编号查找视图
     */
    @Query("MATCH (p:PartView {partNumber: $partNumber}) RETURN p")
    List<PartView> findByPartId(String partNumber);

    /**
     * 根据视图类型查找视图
     */
    @Query("MATCH (p:PartView {viewType: $viewType}) RETURN p")
    List<PartView> findByViewType(String viewType);
    //查询零件、条件查询
    @Query("MATCH (p:PartView) " +
            "WHERE ($partNumber IS NULL OR p.partNumber = $partNumber) " +
            "AND ($viewType IS NULL OR p.viewType = $viewType) " +
            "AND ($viewImageUrl IS NULL OR p.viewImageUrl = $viewImageUrl) " +
            "AND ($description IS NULL OR p.description = $description) " +
            "RETURN p")
    List<PartView> findViewList(
            @Param("partNumber") String partNumber,
            @Param("viewType") String viewType,
            @Param("viewImageUrl") String viewImageUrl,
            @Param("description") String description);
    //查询所有的零件编号
    @Query("MATCH (p:Part) RETURN p.partNumber")
    List<String> findAllPartNumber();

    /**
     * 根据视图 ID 删除视图
     */
    @Query("MATCH (p:PartView) WHERE p.viewId IN $viewIds DETACH DELETE p RETURN count(p)")
    int deleteByViewIds(@Param("viewIds") List<Long> viewIds);

    /**
     * 添加视图
     */
    @Query("CREATE (p:PartView {viewNumber: $viewNumber,partNumber: $partNumber, viewType: $viewType, viewImageUrl: $viewImageUrl, description: $description}) RETURN p.viewNumber")
    String addPartView(@Param("viewNumber") String viewNumber,
                     @Param("partNumber")String partNumber, @Param("viewType")String viewType,
                     @Param("viewImageUrl")String viewImageUrl, @Param("description")String description);

    /**
     * 更新视图
     */
    @Query("MATCH (p:PartView) WHERE p.viewNumber = $viewNumber SET p.viewImageUrl = $viewImageUrl, p.description = $description RETURN count(p)")
    int updatePartView(@Param("viewNumber") String viewNumber,
            @Param("viewImageUrl")String viewImageUrl,
                       @Param("description")String description);
//    根据视图number删除视图
    @Query("MATCH (p:PartView) WHERE p.viewNumber = $viewNumber DELETE p RETURN count(p)")
    int delete(@Param("viewNumber") String viewNumber);
}
